module.exports = function ($scope, $resource, $modal, toaster) {


    init();
    //初始化加载事件列表,包含任务信息
    function init(){
        $scope.formData = {
            'loading': true
        }
        $resource('/api/ac/sc/eventTaskService/getEventList', {}, {}).save({}, function(res){
            if (res.errcode === 0){
                $scope.eventList = res.data.event;
                angular.forEach($scope.eventList, function(item, index){
                    item.open = true; //控制展开关闭箭头
                })
                $scope.taskList = res.data.task;
                $scope.formData.loading = false;
            } else {
                toaster.error({title: '', body: '加载事件列表失败, 请刷新页面'});
            }
        });
    };

    //添加事件
    $scope.addEvent = function(){
        var modalInstance = $modal.open({
			template: require('../views/eventInfo.html'),
			controller: 'eventInfo',
			size: 'lg',
			resolve: {
				event: function () {
					return {};
				},
				updateFlag: function () {
					return false;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){
            result.open = true;
            $scope.eventList.unshift(result);
		});
    }

    //修改事件信息
    $scope.edit = function(event){
		var modalInstance = $modal.open({
			template: require('../views/eventInfo.html'),
			controller: 'eventInfo',
			size: 'lg',
			resolve: {
				event: function () {
					return event;
				},
				updateFlag: function () {
					return true;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){

		});
    }

    //删除事件,确认无任务后可删除
    $scope.delete = function(eventId, index){
        if(confirm('无任务的事件才能删除, 确认删除此事件吗?')){
            $resource('/api/ac/sc/eventTaskService/deleteEvent', {}, {}).save({'id': eventId}, function(res){
                if (res.errcode === 0){
                    toaster.success({title: '', body: '删除成功'});
                    $scope.eventList.splice(index, 1);
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }
    }



    
    //添加任务
    $scope.addTask = function(obj){
        var modalInstance = $modal.open({
			template: require('../views/taskInfo.html'),
			controller: 'taskInfo',
			size: 'lg',
			resolve: {
				task: function () {
					return {'event_id': obj.id};
				},
				updateFlag: function () {
					return false;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){
            init();
		});
    }

    //修改任务信息
    $scope.editTask = function(task){
		var modalInstance = $modal.open({
			template: require('../views/taskInfo.html'),
			controller: 'taskInfo',
			size: 'lg',
			resolve: {
				task: function () {
					return task;
				},
				updateFlag: function () {
					return true;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result){
            init();
		});
    }

    //开启or停用任务
    $scope.openANDstop = function(task){
        var para = {
            'id': task.id,
            'oper_type': task.oper_type,
            'del_flag': task.del_flag == '0' ? '1' : '0'
        }
        var msg = task.del_flag == '0' ? '确认停用此任务吗？':'确认开启此任务呀吗？';
        if(confirm(msg)){
            $resource('/api/ac/sc/eventTaskService/saveTask', {}, {}).save(para, function(res){
                if (res.errcode === 0){
                    if(res.data.id == '0'){
                        toaster.error({title: '', body: '失败, 请重试'});
                    } else {
                        toaster.success({title: '', body: '成功'});
                        task.del_flag = para.del_flag;
                    }
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }

    }

    //删除任务
    $scope.deleteTask = function(taskId, index){
        if(confirm('确认删除此任务吗?')){
            $resource('/api/ac/sc/eventTaskService/deleteTask', {}, {}).save({'id': taskId}, function(res){
                if (res.errcode === 0){
                    toaster.success({title: '', body: '删除成功'});
                    init();
                } else {
                    toaster.error({title: '', body: '删除失败'});
                }
            });
        }
    }

};